#include "TestState.h"
#include "Module.h"
#include "Sprite.h"
#include "ResMgr.h"
#include "Texture.h"
#include "Rect.h"
#include "Const.h"
#include "Animation.h"
#include "AnimationSprite.h"
#include "Utils.h"
#include "Graphics.h"
#include "Button.h"
#include "GameUtils.h"

TestState::TestState()
{
	xx = yy = 0;
	button = new Button(0, 0, 100, 100);
	aniSprite = new AnimationSprite();
	ani = new Animation(0);
	ani->GenerateAuto(ResMgr::GetInstance()->GetTexture(TID_FRUIT),32, 32, 4, 0, 6, 0);
	aniSprite->AddAnimation(ani);
	button->SetImageForTouchUp(aniSprite, 0, 1);
	button->SetImageForTouchDown(aniSprite, 0, 2);
}

void TestState::TouchEventButton(Button* sender, int event)
{

}
TestState::~TestState()
{
	DELETE_SAFELY(aniSprite);
}

/*void TestState::TouchUpButton()
{
	PRINT("Button click");
}*/

void TestState::Init()
{
	t = new Texture(ROOT_IMG_DIR"test.png", 0);

	ResMgr* resMgr = ResMgr::GetInstance();
	resMgr->AddTexture(t);
	ani = new Animation(0);
	aniSprite = new AnimationSprite();

	ani->GenerateAuto(t, 112, 44, 2, 0, 1, 1.0f);
	aniSprite->AddAnimation(ani);

	ani = new Animation(1);

	ani->GenerateAuto(t, 112, 44, 2, 2, 3, 1.0f);

	aniSprite->AddAnimation(ani);
	aniSprite->SetCurAnimation(0);
}
void TestState::Update()
{
	aniSprite->Update();
}

void TestState::Render()
{
	button->Render();
}

void TestState::Exit()
{

}

void TestState::TouchUp (int x, int y)
{
	if(button->GetIsTouchDown() == true){
		button->TouchUp();
		TouchEventButton(button, BTE_TOUCH_UP);
	}
}

void TestState::TouchDown (int x, int y)
{
	if(IsPointInRect(x, y, button->GetRect()))
	{
		button->TouchDown();
		TouchEventButton(button, BTE_TOUCH_DOWN);
	}
}

void TestState::TouchMove (int x, int y)
{
	xx = x;
	yy = y;
}